import { Injectable } from '@angular/core';

@Injectable()
export class LocalStorageService {

    read(key: string): string {
        let text: string = localStorage.getItem(key);

        if (text === null || typeof(text) === undefined || text === 'undefined') {
            // this.logger.warn('提示信息');
            return null;
        } else {
            return text;
        }

    }

    readObject<T>(key: string): T {
        let text = this.read(key);
        let data: T;
        try {
            data = <T>JSON.parse(text);
        } catch (error) {
            // this.logger.error('提示信息')
            data = null;
        }
        return data;
    }

    write(key: string, data: string) {
        localStorage.setItem(key, data);
    }

    writeObject(key: string, obj) {
        let text =JSON.stringify(obj);
        this.write(key, text);
    }
}
